package com.wangchao.administrator.zenmechi.ui.fragment.caipuitemfragment;

import android.content.Context;

import com.wangchao.administrator.zenmechi.bean.caipudetailbean.CaipuDetail;
import com.wangchao.administrator.zenmechi.bean.caipudetailbean.CaipuDetailDataBean;
import com.wangchao.administrator.zenmechi.config.UrlConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;

/**
 * Created by Administrator on 2016/10/17.
 */
public class CaipuItemPresenter implements CaipuItemContract.Presenter {

    private CaipuItemContract.View view;
    private CaipuItemContract.Model model;

    public CaipuItemPresenter(CaipuItemContract.View view) {
        this.view = view;
        this.model = new CaipuItemModel();
    }

    private String id;
    private int pageNum = UrlConfig.DefaultVaule.PAGE_NUM,
            pageSize = UrlConfig.DefaultVaule.PAGE_SIZE;
    private Map<String,String> map = new HashMap<>();
    private List<CaipuDetailDataBean> beans;
    private MyAdapterForCaipuList adapter;


    @Override
    public void getDetail(final Context context) {
        id = view.getIds();
        map.put(UrlConfig.Params.PAGE,pageNum+"");
        map.put(UrlConfig.Params.PAGE_SIZE,pageSize+"");

        model.getDetail(id, map, new Subscriber<CaipuDetail>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(CaipuDetail caipuDetail) {
                if (pageNum == 1){
                    beans = caipuDetail.getResult().getData();
                    //Log.i("tag","----------"+beans.size());
                    adapter = new MyAdapterForCaipuList(context,beans);
                    view.getDetail(adapter);
                }else {
                    beans.addAll(caipuDetail.getResult().getData());
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    public void PullDownRefresh(Context context){
        pageNum = 1;
        getDetail(context);
    }

    public void PullUpRefresh(Context context){
        pageNum ++;
        getDetail(context);
    }


    public String getItemId(int i){
        return beans.get(i-1).getId()+"";
    }
}
